//Page Object
const url = require('../../config/config.js')
Page({
    data: {
        goodList: [],
        foCuse: false,
        inputValue: '',

    },
    pageVo: { size: 10, indexPageNum: 1 },
    orderLikeVo: { customer: '', admin: "", typeNum: "", },
    totalPages: 0,


    // 输入框触发的事件
    handleSearch(e) {
        console.log(e)
            //解构赋值
        const { value } = e.detail;
        // 合法性的验证
        if (!value.trim()) {
            //空字符串的时候
            this.setData({
                goodList: [],
                foCuse: false
            })
            return
        }
        this.setData({
            foCuse: true
        })
        this.orderLikeVo.customer = value;
        this.orderLikeVo.admin = value;
        this.orderLikeVo.typeNum = value;
        // 发送数据
        this.getListData()

    },
    // 请求数据
    getListData() {
        const header = {
            'content-type': 'application/json',
            'Cookie': "JSESSIONID=" + wx.getStorageSync("token") //读取cookie
        }
        wx.request({
            url: url.b.orderGoodsList,
            data: { pageVo: this.pageVo, orderLikeVo: this.orderLikeVo },
            header: header,
            method: 'GET',
            dataType: 'json',
            responseType: 'text',
            success: (result) => {
                try {
                    // 获取总条数
                    const total = result.data.data.pageResult.totalCount
                        // 计算总页数
                    this.totalPages = Math.ceil(total / this.pageVo.size);
                    console.log()
                    wx.showLoading({
                        title: "加载中",
                        mask: true,
                    });
                    this.setData({
                        // 堆叠人书
                        goodList: [...this.data.goodList, ...result.data.data.resultList]
                    })
                    wx.hideLoading();
                } catch (error) {
                    wx.showToast({
                        title: '登陆过期',
                        icon: 'none',
                    });
                    wx.removeStorageSync({ token: "token" });
                    wx.navigateTo({
                        url: '/pages/login/index',

                    });
                }

            },
            fail: (err) => {
                console.log(err)
            }
        });
    },
    // 取消按钮
    handleCancel() {
        this.setData({
            inputValue: '',
            foCuse: false,
            goodList: []
        })

    },
    // 触底加载事件
    onReachBottom() {
        //如果当前页数大于等于总页数，则证明没有数据了 
        if (this.pageVo.indexPageNum >= this.totalPages) {

            wx.showToast({
                title: '没有数据了QAQ',
                icon: 'none',
            });
        } else {
            this.pageVo.indexPageNum++;
            this.getListData();
        }


    }

});